package com.mixi.feedreader;

import java.text.AttributedCharacterIterator.Attribute;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;
import android.widget.FrameLayout;

public class TagHandler extends DefaultHandler{
	
	private boolean title = false;
	private boolean link = false;
	private boolean description = false; 
	
	
	String temp[] = new String[3];
	boolean flag[] = new boolean[3];
	 
	private ParsedTag ptx = new ParsedTag();
	
	public ParsedTag getParseTag(){
		return this.ptx;
	}
	
	
	@Override
	public void startElement(String nameSpaceURI,String localName,String gName,Attributes attr)throws SAXException {
		if(localName.equals("title")){// outter tag name 
			this.title = true;
		}else if(localName.equals("link")){ //inner tag name
			this.link = true;
		}else if(localName.equals("description")){
			this.description = true;
		}
		
//		Log.i(FeedReaderActivity.tag, "start to read element ");
	}
	
	
	@Override
	public void endElement(String nameSpaceURI,String localName,String gName)throws SAXException {
		if(localName.equals("title")){// outter tag name 
			this.title = false;
		}else if(localName.equals("link")){ //inner tag name
			this.link = false;
		}else if(localName.equals("description")){
			this.description = false;
		}
		
		//Log.i("xct", "localName :: "+localName+" G-Name :: "+gName);
		
	}
	
	
	 @Override
	 public void characters(char ch[], int start, int length) {
		 Holder hx = null;
		 
//		 flag[0]=flag[1]=flag[2]= false;
		 
		 
		 if(this.title){
		 		Log.i(FeedReaderActivity.tag, (FeedReaderActivity.cnt++)+"set Text if is title "+new String(ch, start, length));
		 		ptx.setExtractedString(new String(ch, start, length));
		 		
		 		temp[0] = new String(ch, start, length);
		 		flag[0] = this.title;
//		 		hx.setTitle(new String(ch, start, length));
         }
		 
		 if(this.link){
		 		Log.i(FeedReaderActivity.tag, "set Text if is link "+new String(ch, start, length));
		 		ptx.setExtractedString(new String(ch, start, length));
		 		
		 		temp[1] = new String(ch, start, length);
		 		flag[1] = this.link;
//		 		hx.setLink(new String(ch, start, length));
         }
		 
		 if(this.description){
		 		Log.i(FeedReaderActivity.tag, "set Text if is description "+new String(ch, start, length));
		 		ptx.setExtractedString(new String(ch, start, length));
		 		
		 		temp[2] = new String(ch, start, length);
		 		flag[2] = this.description;
//		 		hx.setDescription(new String(ch, start, length));
		 }
		 
		 
		 if(flag[0]&&flag[1]&&flag[2])
		 {
			 Log.i(FeedReaderActivity.tag, "push");
			 hx = new Holder();
			 hx.setTitle(temp[0]);
			 hx.setLink(temp[1]);
			 hx.setDescription(temp[2]);
			 FeedReaderActivity.hList.add(hx);
			 flag[0]=flag[1]=flag[2]= false;
			 
		 }
//		 if(this.title && this.link && this.description) FeedReaderActivity.hList.add(hx);

	          
	 }
	
	
	
	
	
	
	
	@Override
	public void startDocument() throws SAXException{
		this.ptx = new ParsedTag();
	}
	
	@Override
	public void endDocument() throws SAXException{
		
	}
	
	

}
